package cn.yshujia.utils;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.vo.PageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YShuJia
 * @create 2025/1/10
 * @description PageUtils
 */

public class PageUtils {
	
	public static <T> PageVO<T> getPage(PageDTO dto, List<T> list) {
		if (ObjectUtils.isEmpty(list)) {
			list = new ArrayList<>();
		}
		PageVO<T> page = new PageVO<>();
		page.setSize(dto.getSize());
		page.setCurrent(dto.getCurrent());
		page.setIsEmpty(list.size() < dto.getSize());
		page.setList(list);
		return page;
	}
	
	public static <T> PageVO<T> getPage(PageDTO dto, List<T> list, Long total) {
		PageVO<T> page = getPage(dto, list);
		page.setTotal(total);
		return page;
	}
	
	private static <T> QueryWrapper<T> createQueryWrapper(PageDTO dto, Map<String, String> fieldMapping) {
		if (dto == null || ObjectUtils.isEmpty(dto.getParam())) {
			return new QueryWrapper<>();
		}
		Map<String, Object> param = dto.getParam();
		QueryWrapper<T> wrapper = new QueryWrapper<>();
		
		for (Map.Entry<String, String> entry : fieldMapping.entrySet()) {
			String paramKey = entry.getKey();
			String field = entry.getValue();
			if (!ObjectUtils.isEmpty(param.get(paramKey))) {
				if ("name".equals(paramKey)) {
					wrapper.like(field, param.get(paramKey));
				} else {
					wrapper.eq(field, param.get(paramKey));
				}
			}
		}
		return wrapper;
	}
	
	
	/**
	 * @param dto: 分页参数
	 *             [dto] * @return QueryWrapper<T>
	 * @author YShuJia
	 * @description 键为前端传的参数名，值为映射的数据库字段名
	 * @create 2025/3/6 12:54
	 */
	public static <T> QueryWrapper<T> createArticleWrapper(PageDTO dto) {
		Map<String, String> fieldMapping = new HashMap<>();
		fieldMapping.put("labelId", "label_id");
		fieldMapping.put("name", "title");
		fieldMapping.put("status", "status");
		return createQueryWrapper(dto, fieldMapping);
	}
	
	public static <T> QueryWrapper<T> createRouterWrapper(PageDTO dto) {
		Map<String, String> fieldMapping = new HashMap<>();
		fieldMapping.put("level", "level");
		fieldMapping.put("isAdmin", "is_admin");
		fieldMapping.put("status", "status");
		return createQueryWrapper(dto, fieldMapping);
	}
	
	public static <T> QueryWrapper<T> createLabelWrapper(PageDTO dto) {
		Map<String, String> fieldMapping = new HashMap<>();
		fieldMapping.put("labelId", "id");
		fieldMapping.put("classifyId", "classify_id");
		fieldMapping.put("name", "name");
		fieldMapping.put("status", "status");
		return createQueryWrapper(dto, fieldMapping);
	}
	
	public static <T> QueryWrapper<T> createResourcesWrapper(PageDTO dto) {
		Map<String, String> fieldMapping = new HashMap<>();
		fieldMapping.put("labelId", "label_id");
		fieldMapping.put("name", "name");
		fieldMapping.put("status", "status");
		return createQueryWrapper(dto, fieldMapping);
	}
	
	public static <T> QueryWrapper<T> createWebsiteWrapper(PageDTO dto) {
		return createArticleWrapper(dto);
	}
	
	public static <T> QueryWrapper<T> createAlbumWrapper(PageDTO dto) {
		return createArticleWrapper(dto);
	}
	
	public static <T> QueryWrapper<T> createUserWrapper(PageDTO dto) {
		Map<String, String> fieldMapping = new HashMap<>();
		fieldMapping.put("roleId", "role_id");
		fieldMapping.put("name", "username");
		fieldMapping.put("status", "status");
		fieldMapping.put("gender", "gender");
		return createQueryWrapper(dto, fieldMapping);
	}
}
